﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.ProductPlan;

namespace SmartProduction.Write.API.Application.CommandHandlers.ProductPlans
{
    public class ProductPlanUpdateCommandHandler:IRequestHandler<ProductPlanUpdateCommand,APIResult<object>>
    {
        /// <summary>
        /// 主生产计划仓储
        /// </summary>
        private readonly IBaseRepository<Domain.yjh.ProductPlan> product;
        /// <summary>
        /// mapper映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="product">主生产计划仓储</param>
        /// <param name="mapper">mapper映射</param>
        public ProductPlanUpdateCommandHandler(IBaseRepository<Domain.yjh.ProductPlan> product, IMapper mapper)
        {
            this.product = product;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(ProductPlanUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

          

            var entity = mapper.Map<Domain.yjh.ProductPlan>(request);

            var r = product.UpdateAsync(entity).Result;

            if (r > 0)
            {
                result.Code = APIEnums.修改成功;
                result.Msg = APIEnums.修改成功.ToString();
                result.data = r;
            }
            else
            {
                result.Code = APIEnums.修改失败;
                result.Msg = APIEnums.修改失败.ToString();
                result.data = r;
            }

            return await Task.FromResult(result);
        }
    }
}
